package restaurant;

/**
 * Items Class stores and manipulates menu item objects.
 * 
 * @author SG350, CT267, MSM41.
 */
import java.util.TreeSet;

public class Items {

	/**
	 * Field variables.
	 */
	private final String space = "  ";
	private IOsystem io = null;
	private TreeSet<MenuItem> treeSet = null;

	/**
	 * Constructor creates new IOsystem object and populates TreeSet.
	 */
	public Items() {
		io = new IOsystem();
		treeSet = io.readMenuFile("menu_input.txt");
	}

	/**
	 * TreeSet is used to hold details from input file to avoid duplication of
	 * menu items and display them in alphabetical order.
	 * 
	 * @return treeSet
	 */
	public TreeSet<MenuItem> getMenuTreeSet() {
		return treeSet;
	}

	/**
	 * Read menu input file and to hold the category, dish name and price for
	 * all menu item and return these in concatenated string.
	 * 
	 * @return output
	 */
	public String getMenuItems() {
		String output = "";
		output = "MENU" + "\n" + "====" + "\n" + "STARTER" + "\n"
				+ listStarters() + "\n" + "MAIN" + "\n" + listMains() + "\n"
				+ "DESSERT" + "\n" + listDesserts() + "\n" + "DRINKS" + "\n"
				+ listDrinks();
		return output;
	}

	/**
	 * Read details of Starter items from menu input file.
	 * 
	 * @return output
	 */
	public String listStarters() {
		String output = "";
		for (MenuItem mi : treeSet) {
			if (mi.getCategory().contains("Starter")) {
				output += space
						+ String.format("%-15s", mi.getDish().toUpperCase())
						+ "£" + String.format("%.2f", mi.getCost()) + "\n";
			}
		}
		return output;
	}

	/**
	 * Read details of Main items from menu input file.
	 * 
	 * @return output
	 */
	public String listMains() {
		String output = "";
		for (MenuItem mi : treeSet) {
			if (mi.getCategory().contains("Main")) {
				output += space
						+ String.format("%-15s", mi.getDish().toUpperCase())
						+ "£" + String.format("%.2f", mi.getCost()) + "\n";
			}
		}
		return output;
	}

	/**
	 * Read details of Dessert items from menu input file.
	 * 
	 * @return output
	 */
	public String listDesserts() {
		String output = "";
		for (MenuItem mi : treeSet) {
			if (mi.getCategory().contains("Dessert")) {
				output += space
						+ String.format("%-15s", mi.getDish().toUpperCase())
						+ "£" + String.format("%.2f", mi.getCost()) + "\n";
			}
		}
		return output;
	}

	/**
	 * Read details of Drinks from menu input file.
	 * 
	 * @return output
	 */
	public String listDrinks() {
		String output = "";
		for (MenuItem mi : treeSet) {
			if (mi.getCategory().contains("Drinks")) {
				output += space
						+ String.format("%-15s", mi.getDish().toUpperCase())
						+ "£" + String.format("%.2f", mi.getCost()) + "\n";
			}
		}
		return output;
	}
}
